package visao.motorista;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import modelo.dominio.Motorista;
import modelo.persistencia.ControleAcesso;
import modelo.persistencia.PersistenciaMotorista;

/*
 * Classe que cria a interface de listagem de motoristas
 */
public class InterfaceListagemMotoristas extends javax.swing.JFrame{
	
	private final PersistenciaMotorista persistenciaMotorista = new PersistenciaMotorista();
	/*
	 * Construtor da Classe Interface Listagem Motorista
	 * @throws IOException
	 */
	public InterfaceListagemMotoristas() throws IOException {
		initComponents();
		this.setTitle("Lista de Motoristas");
		
		String nome, telefone, endereco, cnh;
		long  cpf;
		ArrayList<Motorista> Motorista = persistenciaMotorista.getListMotorista();
		for(Motorista motorista: Motorista){
			nome = motorista.getNome();
			cpf = motorista.getCpf();
			telefone = motorista.getTelefone();
			endereco = motorista.getEndereco();
			cnh = motorista.getCnh();
			((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{cnh,nome,cpf,telefone,endereco});
		}
	}
	// fim construtor
	
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
			  
            },
            new String [] {
                "Nº cnh", "Nome", "CPF", "Telefone", "Endereço" 
            }
        ){
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Long.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            
            
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        DefaultTableCellRenderer esquerda = new DefaultTableCellRenderer();  
        DefaultTableCellRenderer centralizado = new DefaultTableCellRenderer();  
        esquerda.setHorizontalAlignment(SwingConstants.LEFT);  
        centralizado.setHorizontalAlignment(SwingConstants.CENTER);  
        jTable1.getColumnModel().getColumn(0).setCellRenderer(esquerda);  
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(1).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(11);
        jTable1.getColumnModel().getColumn(2).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(15);
        jTable1.getColumnModel().getColumn(3).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(15);
        jTable1.getColumnModel().getColumn(4).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(4).setPreferredWidth(110);
	   
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 459, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton1.setText("Logoff");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLogoff(evt);
            }
        });

        jButton2.setText("Voltar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoltar(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 587, Short.MAX_VALUE)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>

	/*
	 * Método do botão voltar
	 * @param evt
	 */
	private void jButtonVoltar(java.awt.event.ActionEvent evt) {                               
		dispose();
		ControleAcesso.voltarMenuPrincipal();
	}                              
	// fim método

	/*
	 * Método do botão log off
	 */
	private void jButtonLogoff(java.awt.event.ActionEvent evt) {                               
		dispose();
		ControleAcesso.logoff();
	}
	// fim do método
	
	
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
}
// fim classe
